package 力扣_树算法.路径;

import java.util.*;

/**113.路径总和 II
 *
 * 257. 二叉树的所有路径
 * @author zx
 * @create 2022-04-18 10:22
 */
public class Num113 {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        helper(root,targetSum);
        return res;
    }
    private void helper(TreeNode root, int targetSum){
        if(root == null){
            return;
        }
        path.offer(root.val);
        targetSum -= root.val;
        if(root.left == null && root.right == null && targetSum == 0){
            res.add(new ArrayList<>(path));
        }
        helper(root.left,targetSum);
        helper(root.right,targetSum);
        path.removeLast();
        targetSum += root.val;
    }
}
